<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }

        body {
            font-family: 'Helvetica Neue', Arial, sans-serif;
            background-color: #f8f9fa;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            padding: 20px;
        }

        .register-container {
            background-color: white;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
            width: 100%;
            max-width: 420px;
        }

        .register-header {
            text-align: center;
            margin-bottom: 30px;
        }

        .register-header h1 {
            color: #2c3e50;
            font-size: 24px;
            margin-bottom: 10px;
        }

        .register-header p {
            color: #7f8c8d;
            font-size: 14px;
        }

        .form-group {
            margin-bottom: 20px;
        }

        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: #34495e;
            font-size: 14px;
        }

        .form-control {
            width: 100%;
            padding: 12px 15px;
            border: 1px solid #ddd;
            border-radius: 6px;
            font-size: 15px;
            transition: border-color 0.3s;
        }

        .form-control:focus {
            border-color: #3498db;
            outline: none;
            box-shadow: 0 0 0 3px rgba(52, 152, 219, 0.1);
        }

        .password-container {
            position: relative;
        }

        .toggle-password {
            position: absolute;
            right: 12px;
            top: 50%;
            transform: translateY(-50%);
            cursor: pointer;
            color: #7f8c8d;
            font-size: 14px;
        }

        .btn-register {
            width: 100%;
            padding: 14px;
            background-color: #3498db;
            color: white;
            border: none;
            border-radius: 6px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background-color 0.3s;
            margin-top: 10px;
        }

        .btn-register:hover {
            background-color: #2980b9;
        }

        .login-link {
            text-align: center;
            margin-top: 20px;
            font-size: 14px;
            color: #7f8c8d;
        }

        .login-link a {
            color: #3498db;
            text-decoration: none;
            font-weight: 500;
        }

        .login-link a:hover {
            text-decoration: underline;
        }

        .error-message {
            color: #e74c3c;
            font-size: 13px;
            margin-top: 5px;
            display: none;
        }

        .form-control.error {
            border-color: #e74c3c;
        }
    </style>
</head>
<body>
<div class="register-container">
    <div class="register-header">
        <h1>创建新账户</h1>
        <p>立即注册，享受我们的服务</p>
    </div>

    <form id="registerForm">
        <div class="form-group">
            <label for="userName">用户名</label>
            <input type="userName" id="userName" class="form-control" placeholder="请输入用户名" required>
            <div class="error-message" id="username-error">用户名不能为空</div>
        </div>

        <div class="form-group">
            <label for="password">密码</label>
            <div class="password-container">
                <input type="password" id="password" class="form-control" placeholder="请输入密码（至少8位）" required>
                <span class="toggle-password" onclick="togglePasswordVisibility()">显示</span>
            </div>
        </div>

        <div class="form-group">
            <label for="pid">身份证号码</label>
            <input type="pid" id="pid" class="form-control" placeholder="请输入身份证号码" required>
            <div class="error-message" id="phone-error">请输入有效的手机号码</div>
        </div>

        <button type="submit" class="btn-register">注册</button>
    </form>

    <div class="login-link">
        已有账号？<a href="login.html">立即登录</a>
    </div>
</div>

<script>
    // 切换密码可见性
    function togglePasswordVisibility() {
        const passwordInput = document.getElementById('password');
        const toggleBtn = document.querySelector('.toggle-password');

        if (passwordInput.type === 'password') {
            passwordInput.type = 'text';
            toggleBtn.textContent = '隐藏';
        } else {
            passwordInput.type = 'password';
            toggleBtn.textContent = '显示';
        }
    }

    document.getElementById('registerForm').addEventListener('submit', function (event) {
        event.preventDefault(); // 阻止表单默认提交行为，防止页面刷新
        const userName = document.getElementById('userName').value;
        const password = document.getElementById('password').value;
        const pid = document.getElementById('pid').value;

        // 创建XMLHttpRequest对象
        const xhr = new XMLHttpRequest();
        xhr.open('POST', 'sysUser/addUser', "success"); // 替换为实际注册接口地址，true表示异步请求
        xhr.setRequestHeader('Content-Type', 'application/json'); // 设置请求头，表明发送JSON数据

        // 构建要发送的数据
        const data = JSON.stringify({
            userName: userName,
            password: password,
            pid: pid
        });

        /*xhr.onreadystatechange = function () {
            if (xhr.readyState === 4 && xhr.status === 200) {
                const response = JSON.parse(xhr.responseText);
                if (response.status ==='success') {
                    alert('注册成功');
                } else {
                    alert('注册失败：' + response.message);
                }
            }
        };*/

        xhr.onreadystatechange = function () {
            if (xhr.readyState === 4) {
                if (xhr.status === 200) {
                    try {
                        const response = JSON.parse(xhr.responseText);
                        if (response.success === true) { // 检查 success 是否为 true
                            alert('注册成功');
                            window.location.href="index.html";
                        } else {
                            if (response.msg) { // 检查 msg 字段是否存在
                                alert('注册失败：' + response.msg);
                            } else {
                                alert('注册失败，未获取到错误信息');
                            }
                        }
                    } catch (error) {
                        console.error('解析 JSON 出错:', error);
                        alert('注册过程中出现错误，请检查服务器响应');
                    }
                } else {
                    alert('请求失败，状态码:' + xhr.status);
                }
            }
        };

        xhr.send(data);
    });

    </script>
</body>
</html>